﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Spire.Xls;
using System.IO;
using System.Data.SqlClient;
using Net.Orm;
using Net.Studio.Forms;

namespace DataModelWF
{
    public partial class Form1 : Form
    {
        private DBModel dbModel = null;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //创建Workbook对象并加载Excel文档
                Workbook workbook = new Workbook();
                string path = Directory.GetCurrentDirectory();
                string strfile = path + @"\" + "fluxWMS.xlsx";

                memo.Clear();
                memo.AppendText("=============开始处理========================\r\n");
                dbModel =new ExcelModelManager().Load(strfile);
                memo.AppendText("============= 结束处理========================\r\n");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        private void button2_Click(object sender, EventArgs e)
        {
            bool ret = new ExcelModelManager().Save(dbModel);
            if (ret)
                MessageBox.Show("导出成功");
            else
                MessageBox.Show("导出失败");

        }


        private void btnLoad_Click(object sender, EventArgs e)
        {
            IData idata = new DBData();
            string strjson = "RemoteDBConnectionString"; //string.Format("{{\"DBName\":\"DBName\",\"DBNo\":\"DBNo\"}}");
            DBModel dbModel = idata.Load(strjson);
            if (dbModel != null && dbModel.Tables.Count > 0)
            {
                MessageBox.Show("加载成功");
                return;
            }
            MessageBox.Show("加载失败");
        }

        private void btnsave_Click(object sender, EventArgs e)
        {
            //string sqlText = "select top 1 * from BPM_DefineFlowDraft"; 
            //DbSession dbs = new DbSession("RemoteDBConnectionString");
            //DataFactory.remote = dbs;
            //DataResult reslt =  DataFactory.remote.FromSql(sqlText).ToDataTable();
            //int i = 0;
            //return;

            IData idata = new DBData();
            string strjson = "RemoteDBConnectionString";
            DBModel dbModel = idata.Load(strjson);

            bool flg = idata.Save(dbModel);
            if (flg)
            {
                MessageBox.Show("执行成功");
                return;
            }
            MessageBox.Show("执行失败");
        }

    }
}
